package com.hrbxlh.virtualization.controller;

import com.github.pagehelper.PageInfo;
import com.hrbxlh.virtualization.param.CourseQueryParam;
import com.hrbxlh.virtualization.param.ImageAllocationCancelParam;
import com.hrbxlh.virtualization.param.ImageAllocationConfirmParam;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ImageAllocationService;
import com.hrbxlh.virtualization.util.SessionUtil;
import com.hrbxlh.virtualization.vo.ChapterImageAllocationVO;
import com.hrbxlh.virtualization.vo.CourseImageAllocationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/image/allocate")
public class ImageAllocationController {

    @Autowired
    private ImageAllocationService imageAllocationService;

    /**
     * 分页查询课程的镜像分配情况
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result<PageInfo<CourseImageAllocationVO>> listImageAllocations(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "8") Integer pageSize,
            CourseQueryParam courseQueryParam) {

        courseQueryParam.setStuorteaId(SessionUtil.getStuorteaId());
        PageInfo<CourseImageAllocationVO> page = imageAllocationService.listImageAllocationsByPage(
                pageNum, pageSize, courseQueryParam);
        return Result.success(page);
    }

    /**
     * 查询课程下每章节的镜像分配情况
     *
     * @param courseId 课程Id
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Result<List<ChapterImageAllocationVO>> queryImageAllocation(Integer courseId) {
        List<ChapterImageAllocationVO> chapterImageAllocationVOList = imageAllocationService.queryImageAllocation(courseId);
        return Result.success(chapterImageAllocationVOList);
    }

    /**
     * 分配镜像
     */
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    public Result confirmImageAllocation(@RequestBody @Valid ImageAllocationConfirmParam imageAllocationConfirmParam) {
        imageAllocationService.confirmImageAllocation(imageAllocationConfirmParam);
        return Result.success();
    }

    /**
     * 取消分配镜像
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public Result cancelImageAllocation(@RequestBody @Valid ImageAllocationCancelParam imageAllocationCancelParam) {
        imageAllocationService.cancelImageAllocation(imageAllocationCancelParam);
        return Result.success();
    }
}
